package view;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.table.*;
import javax.swing.text.JTextComponent;

import model.WebCrawler;

/**
 * This class contains the search / crawl tab, used as a component in the main GUI.
 * @author Audun
 *
 */

public class SearchTab extends JPanel {
	private static final String[] MAX_URLS = { "50", "100", "500", "1000", "10000" };
	private HashMap disallowListCache = new HashMap();
	private JLabel matchesLabel2, searchLabel, statusLabel;
	private JTable table;
	private WebCrawler engine;
	private JPanel searchPane;
	private JTextField searchField;
	private JCheckBox caseSensitive;
	private JComboBox searchSizeComboBox;
	private JButton searchButton;
	private JList resultList;
	private DefaultListModel model;

	public SearchTab() {
		setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MIN_VALUE));
		initiateComponents();
		engine = new WebCrawler(this);
	}

	public void initiateComponents() {
		JPanel searchPanel = createSearchPane();
		JPanel resultPanel = createResultPanel();

		setLayout(new BorderLayout());
		add(searchPanel, BorderLayout.NORTH);
		add(resultPanel, BorderLayout.CENTER);
	}

	private JPanel createSearchPane() {
		searchPane = new JPanel();
		searchLabel = new JLabel();
		statusLabel = new JLabel();
		searchField = new JTextField();
		searchButton = new JButton();

		JLabel searchSizeLabel = new JLabel("Search Size:");
		searchSizeComboBox = new JComboBox(MAX_URLS);
		searchSizeComboBox.setSelectedIndex(2);
		searchSizeComboBox.setEditable(true);
		JPanel searchSizePanel = new JPanel();
		searchSizePanel.add(searchSizeLabel);
		searchSizePanel.add(searchSizeComboBox);

		// filter checkboxes
		caseSensitive = new JCheckBox();
		caseSensitive.setText("Case Sensitive");

		searchButton.setText("Search");
		searchLabel.setText("Search for: ");

		GroupLayout layout = new GroupLayout(searchPane);
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		searchPane.setLayout(layout);

		// HORIZONTAL AXIS
		ParallelGroup hGroup = layout
				.createParallelGroup(GroupLayout.Alignment.LEADING);

		// Create sequential and parallel groups
		SequentialGroup h1 = layout.createSequentialGroup();
		ParallelGroup h2 = layout
				.createParallelGroup(GroupLayout.Alignment.TRAILING);

		// Add a scroll pane and a label to the parallel group h2
		h2.addComponent(statusLabel, GroupLayout.Alignment.LEADING,
				GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE);

		// Create a Parallel group h3
		ParallelGroup h3 = layout
				.createParallelGroup(GroupLayout.Alignment.TRAILING);

		SequentialGroup criteriaGroup = layout.createSequentialGroup();
		criteriaGroup.addComponent(searchSizePanel);
		criteriaGroup.addComponent(caseSensitive);

		SequentialGroup h4 = layout.createSequentialGroup();
		h4.addComponent(searchLabel);
		h4.addComponent(searchField, GroupLayout.DEFAULT_SIZE, 321,
				Short.MAX_VALUE);
		h4.addComponent(searchButton);

		h3.addGroup(criteriaGroup);
		h3.addGroup(h4);
		// Add the group h3 to h2
		h2.addGroup(h3);
		// Add the group h2 to h1
		h1.addGroup(h2);

		// Add the group h1 to the hGroup
		hGroup.addGroup(GroupLayout.Alignment.TRAILING, h1);
		// Create the horizontal group
		layout.setHorizontalGroup(hGroup);

		// VERTICAL AXIS - Sequential Group
		SequentialGroup verticalGroup = layout.createSequentialGroup();

		// Criteria section
		ParallelGroup verticalCriterias = layout
				.createParallelGroup(GroupLayout.Alignment.LEADING);
		verticalCriterias.addComponent(searchSizePanel);
		verticalCriterias.addComponent(caseSensitive);
		verticalGroup.addGroup(verticalCriterias);

		// Searchbar
		ParallelGroup v2 = layout
				.createParallelGroup(GroupLayout.Alignment.BASELINE);
		v2.addComponent(searchLabel);
		v2.addComponent(searchField, GroupLayout.PREFERRED_SIZE,
				GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
		v2.addComponent(searchButton);
		verticalGroup.addGroup(v2);

		// Display Area
		SequentialGroup v1 = layout.createSequentialGroup();
		v1.addComponent(statusLabel);
		verticalGroup.addGroup(v1);

		// set the vertical group
		layout.setVerticalGroup(verticalGroup);
		return searchPane;
	}

	public JPanel createResultPanel() {
		model = new DefaultListModel();
		resultList = new JList(model);
		
		GridBagConstraints constraints;
		GridBagLayout layout = new GridBagLayout();

		JLabel matchesLabel1 = new JLabel("Search Matches:");
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.EAST;
		constraints.insets = new Insets(5, 5, 10, 0);
		layout.setConstraints(matchesLabel1, constraints);
		matchesLabel2 = new JLabel();
		matchesLabel2.setFont(matchesLabel2.getFont().deriveFont(Font.PLAIN));
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		constraints.insets = new Insets(5, 5, 10, 5);
		layout.setConstraints(matchesLabel2, constraints);

		JPanel resultPanel = new JPanel();
		resultPanel.setBorder(BorderFactory
				.createTitledBorder("Search Results"));
		resultPanel.setLayout(new BorderLayout());
		resultPanel.add(new JScrollPane(resultList), BorderLayout.CENTER);
		return resultPanel;
		
	}
	
	public JPanel getSearchPane() {
		return searchPane;
	}

	public void setSearchPane(JPanel searchPane) {
		this.searchPane = searchPane;
	}

	public JLabel getSearchLabel() {
		return searchLabel;
	}

	public void setSearchLabel(JLabel searchLabel) {
		this.searchLabel = searchLabel;
	}

	public JLabel getStatusLabel() {
		return statusLabel;
	}

	public void setStatusLabel(JLabel statusLabel) {
		this.statusLabel = statusLabel;
	}

	public JTextField getSearchField() {
		return searchField;
	}

	public void setSearchField(JTextField searchField) {
		this.searchField = searchField;
	}

	public JCheckBox getCaseSensitive() {
		return caseSensitive;
	}

	public void setCaseSensitive(JCheckBox caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	public JComboBox getSearchSizeComboBox() {
		return searchSizeComboBox;
	}

	public void setSearchSizeComboBox(JComboBox searchSizeComboBox) {
		this.searchSizeComboBox = searchSizeComboBox;
	}

	public void setDisallowListCache(HashMap disallowListCache) {
		this.disallowListCache = disallowListCache;
	}

	public void setMatchesLabel2(JLabel matchesLabel2) {
		this.matchesLabel2 = matchesLabel2;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public void setEngine(WebCrawler engine) {
		this.engine = engine;
	}

	public void setSearchButton(JButton searchButton) {
		this.searchButton = searchButton;
	}



	public static String[] getMaxUrls() {
		return MAX_URLS;
	}

	public HashMap getDisallowListCache() {
		return disallowListCache;
	}

	public JButton getSearchButton() {
		return searchButton;
	}

	public JLabel getMatchesLabel2() {
		return matchesLabel2;
	}

	public WebCrawler getEngine() {
		return engine;
	}

	public JList getResultList() {
		return resultList;
	}

	public DefaultListModel getModel() {
		return model;
	}
}